#include<bits/stdc++.h>
#define PII pair<int,int>
typedef long long ll;
using namespace std;
const int INF=0x3f3f3f3f;
int n,m;
int d[10][505];
PII c[505];
int A[505];
int a[505];
int x[4];
vector<PII>tr[10][505];
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",&A[i]);
    for(int i=1;i<=m;i++)scanf("%d",&c[i].first),c[i].second=i;
    sort(c+1,c+1+m);
    int k=0,Base=1;
    while(Base<n){
        Base*=2;
        k++;
    }
    for(int i=1;i<=k;i++){
        Base/=2;
        for(int j=1;j<=Base;j++){
            scanf("%1d",&d[i][j]);
        }
    }
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d%d",&x[0],&x[1],&x[2],&x[3]);
        for(int i=1;i<=n;i++)a[i]=A[i]^x[i%4];
        int Nowk=0,base=1;
        int ans=0;
        for(int i=1;i<=m;i++){
            while(base<c[i].first){
                base*=2;
                Nowk++;
            }
//            cout<<c[i].first<<' '<<Nowk<<' '<<base<<endl;
            int New=base;
            for(int j=0;j<=Nowk;j++){
                for(int j2=1;j2<=New;j2++)tr[j][j2].clear();
                New/=2;
            }
            New=base;
            for(int j=1;j<=New;j++){
                if(j<=c[i].first)tr[0][j].push_back({j,a[j]});
                else tr[0][j].push_back({j,INF});
            }
            for(int j=1;j<=Nowk;j++){
                New/=2;
                for(int j2=1;j2<=New;j2++){
                    int idx=j2*2-(d[j][j2]^1),fl=0;
                    for(PII tmp:tr[j-1][idx]){
                        if(tmp.second>=j)tr[j][j2].push_back(tmp);
                        else if(tmp.second<j||tmp.second==INF){
                            if(!fl){
                                fl=1;
                                for(PII tmp2:tr[j-1][j2*2-d[j][j2]])tr[j][j2].push_back(tmp2);
                            }
                        }
                    }
//                    cout<<j<<' '<<j2<<':'<<tr[j][j2].size()<<endl;
                }
            }
            int res=0;
            for(PII tmp:tr[Nowk][1])res^=tmp.first;
            ans^=(c[i].second*res);
        }
        printf("%d\n",ans);
    }
	return 0;
}
